package com.newegg.datafeed.adserving.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.newegg.datafeed.adserving.api.annotation.AccessRequired;
import com.newegg.datafeed.adserving.domain.Company;
import com.newegg.datafeed.adserving.model.UserType;
import com.newegg.datafeed.adserving.request.VerifyCompany;
import com.newegg.datafeed.adserving.response.Page;
import com.newegg.datafeed.adserving.response.Response;
import com.newegg.datafeed.adserving.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags="公司接口")
@RestController
@RequestMapping("/api/company")
public class CompanyAPI extends BaseAPI{
	
	@Autowired
	CompanyService service;
	
	@ApiOperation(value="检查公司账户是否存在", consumes="application/json", produces="application/json")
    @RequestMapping(value="/exist", method = RequestMethod.POST)
	@ResponseBody
    public Response<Boolean> exist(@RequestParam String user) throws Exception {
		return SUCCESS(service.checkExist(user));
    }
	
	@ApiOperation(value="公司帐号注册", consumes="application/json", produces="application/json")
    @RequestMapping(value="/register", method = RequestMethod.POST)
	@ResponseBody
    public Response<Company> register(@RequestBody Company company) throws Exception {
		return SUCCESS(service.save(company));
    }
	
	@ApiOperation(value="公司帐号列表", consumes="application/json", produces="application/json")
    @RequestMapping(value="/page", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired(role={UserType.ADMIN, UserType.COMPANY})
    public Response<Page<Company>> page(@RequestParam(defaultValue="1") Integer pageIndex, @RequestParam(defaultValue="10") Integer pageSize) throws Exception {
		return SUCCESS(service.page(pageIndex, pageSize));
    }
	
	@ApiOperation(value="公司帐号审核", consumes="application/json", produces="application/json")
    @RequestMapping(value="/verify", method = RequestMethod.POST)
	@ResponseBody
	@AccessRequired(role={UserType.ADMIN})
    public Response<Void> verify(@RequestBody VerifyCompany verify) throws Exception {
		service.verify(verify.getId(), verify.getUser());
		return SUCCESS();
    }
	
	@ApiOperation(value="公司信息更新", consumes="application/json", produces="application/json")
    @RequestMapping(value="/update", method = RequestMethod.POST)
	@ResponseBody
	@AccessRequired(role={UserType.ADMIN, UserType.COMPANY})
    public Response<Company> update(@RequestBody Company company) throws Exception {
		return SUCCESS(service.update(company));
    }
	
	@ApiOperation(value="删除", consumes="application/json", produces="application/json")
    @RequestMapping(value="/delete", method = RequestMethod.POST)
	@ResponseBody
	@AccessRequired(role={UserType.ADMIN})
    public Response<Void> delete(@RequestParam int id) throws Exception {
		service.delete(id);
		return SUCCESS();
    }
}
